package Json;

import com.google.gson.Gson;
import org.apache.commons.lang3.StringEscapeUtils;
//import org.bouncycastle.jcajce.provider.asymmetric.GOST;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Ajie
 * @date 2022/8/12
 * @function jsonarray 作为 string 拼接到 jsonobject 。最后：存在 jsonarray 存在反斜杠问题。没有解决。 但是可以解析和使用
 * <p>
 * slove: String newJson = StringEscapeUtils.unescapeJson(valueSettigns);会出现不能解析的问题。
 * 直接返回 最外层要用 jsonobject.tostring();
 * {
 * "timestamp": 1660290505578,
 * "message": {
 * "key": "persist.oa.custom.indexdata.action",
 * "value": "[{\"delateAble\":true,\"drawableId\":-1,\"index\":0,\"isWork\":false,\"name\":\"tabbbbbbbbbbbccccc\",\"recordTAG\":0,\"turnDataList\":[{\"action\":\"URL\",\"containUrlTurn\":\"\",\"url\":\"aaaaaaaAa\"},{\"action\":\"APP\",\"cls\":\"com.beeboxes.lawndesk/com.android.quickstep.RecentsActivity\",\"containUrlTurn\":\"bbbbnnnb\"},{\"action\":\"NULL\",\"cls\":\"\",\"containUrlTurn\":\"\",\"url\":\"\"}]}]"
 * },
 * "status": "200",
 * "path": "",
 * "code": 0
 * }
 */
public class JsonArrayAddToObject {
    public static void main(String[] args) throws Exception{
        makeJsonArray();
        String origin = "{\n" +
                "    \"timestamp\": 1660290505578,\n" +
                "    \"message\": {\n" +
                "        \"key\": \"persist.oa.custom.indexdata.action\",\n" +
                "        \"value\": \"[{\\\"delateAble\\\":true,\\\"drawableId\\\":-1,\\\"index\\\":0,\\\"isWork\\\":false,\\\"name\\\":\\\"tabbbbbbbbbbbccccc\\\",\\\"recordTAG\\\":0,\\\"turnDataList\\\":[{\\\"action\\\":\\\"URL\\\",\\\"containUrlTurn\\\":\\\"\\\",\\\"url\\\":\\\"aaaaaaaAa\\\"},{\\\"action\\\":\\\"APP\\\",\\\"cls\\\":\\\"com.beeboxes.lawndesk/com.android.quickstep.RecentsActivity\\\",\\\"containUrlTurn\\\":\\\"bbbbnnnb\\\"},{\\\"action\\\":\\\"NULL\\\",\\\"cls\\\":\\\"\\\",\\\"containUrlTurn\\\":\\\"\\\",\\\"url\\\":\\\"\\\"}]}]\"\n" +
                "    },\n" +
                "    \"status\": \"200\",\n" +
                "    \"path\": \"\",\n" +
                "    \"code\": 0\n" +
                "}";

        System.out.println("JsonArrayAddToObject:main:origin:" + origin);
        String newJson = StringEscapeUtils.unescapeJson(origin);
        System.out.println("JsonArrayAddToObject:main:unescape:" + newJson);
        System.err.println("JsonArrayAddToObject:main:replac:" + origin.replaceAll("&quot;", "\""));

        String jsonArrayStr = "[\n" +
                "        {\n" +
                "            \"delateAble\": true,\n" +
                "            \"drawableId\": -1,\n" +
                "            \"index\": 0,\n" +
                "            \"isWork\": false,\n" +
                "            \"name\": \"taggggggf\",\n" +
                "            \"recordTAG\": 0,\n" +
                "            \"turnDataList\": [\n" +
                "                {\n" +
                "                    \"action\": \"URL\",\n" +
                "                    \"containUrlTurn\": \"\",\n" +
                "                    \"url\": \"httpnjjjjjjkjjkjkkk\"\n" +
                "                },\n" +
                "                {\n" +
                "                    \"action\": \"APP\",\n" +
                "                    \"cls\": \"com.beeboxes.lawndesk/com.google.android.apps.nexuslauncher.search.AppLaunchActivity\",\n" +
                "                    \"containUrlTurn\": \"jjjjj\"\n" +
                "                }\n" +
                "            ]\n" +
                "        }\n" +
                "    ]";

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("key", "abc");
        jsonObject.put("value", jsonArrayStr);

        JSONObject decdJson = new JSONObject(jsonObject.toString());
        System.out.println("JsonArrayAddToObject:main:key:" + decdJson.getString("key"));
        System.out.println("JsonArrayAddToObject:main:value:" + decdJson.getString("value"));

    }

    public static List<String> generatePersonnelNumbers(String startNumber, String endNumber) {
        List<String> personnelNumbers = new ArrayList<>();

        int start = Integer.parseInt(startNumber);
        int end = Integer.parseInt(endNumber);

        for (int i = start; i <= end; i++) {
            String personnelNumber = String.format("%06d", i);
            personnelNumbers.add(personnelNumber);
        }

        return personnelNumbers;
    }

    private static void makeJsonArray() {

        Gson gson = new Gson();
        ArrayList<Map<String, String>> arrayList = new ArrayList<>();
        List<String> personnelNumbers = generatePersonnelNumbers("000001", "001500");

        for (String personnelNumber : personnelNumbers) {
            HashMap<String, String> map = new HashMap<>();
            map.put("personNOO",personnelNumber);
            arrayList.add(map);
        }
        System.err.println("JsonArrayAddToObject:makeJsonArray:>>>"+ gson.toJson(arrayList));
    }
}
